import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:gobelieve/common/style/style.dart';
import 'package:gobelieve/common/utils/global.dart';
import 'package:gobelieve/model/conversation.dart';
import 'package:gobelieve/pages/message_page/conversation_item.dart';
import 'package:gobelieve/provider/im_service.dart';
import 'package:provider/provider.dart';

enum Device { MAC, WIN }

class _DeviceinfoItem extends StatelessWidget {
  _DeviceinfoItem({this.device: Device.MAC});
  final Device device;

  String get devicename {
    return device == Device.WIN ? 'Windows' : 'Mac';
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.all(10.0),
      // color: Color(AppColors.DeviceInfoItemBg),
      decoration: BoxDecoration(
          color: Color(AppColors.DeviceInfoItemBg),
          border: Border(
              bottom: BorderSide(
                  width: 0.5, color: Color(AppColors.DividerColor)))),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.start,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: <Widget>[
          SizedBox(
            width: ScreenUtil().setWidth(30),
          ),
          device == Device.WIN
              ? new Icon(
                  ICons.WINDOWS,
                  size: ScreenUtil().setSp(40.0),
                )
              : new Icon(
                  ICons.MAC,
                  size: ScreenUtil().setSp(40.0),
                ),
          SizedBox(
            width: ScreenUtil().setWidth(50),
          ),
          Text('$devicename 微信已登陆,手机通知已关闭',
              style: TextStyle(
                fontSize: ScreenUtil().setSp(24.0),
                color: Color(AppColors.DeviceInfoItemText),
              ))
        ],
      ),
    );
  }
}

class MessagePage extends StatefulWidget {
  @override
  _CreateMessagePage createState() => new _CreateMessagePage();
}

class _CreateMessagePage extends State<MessagePage> {
  @override
  Widget build(BuildContext context) {
    Future.delayed(Duration(milliseconds: 200), () async {
      Provider.of<ImService>(context).init(G.user.data.token);
    });

    List<Conversation> conversationList =
        Provider.of<ImService>(context).conversationList;
    var length = conversationList.length + 1;
    // print("会话列表长度： $length");
    return Container(
        child: ListView.builder(
      itemBuilder: (BuildContext context, int index) {
        if (index == 0) {
          return _DeviceinfoItem();
          // } else if (index < conversationList.length + 1) {
          //   return ConversationItem(conversationList[index - 1],
          //       index - 1, conversationList[index - 1].type - 1);
        } else {
          return ConversationItem(conversationList[index - 1], index - 1,
              conversationList[index - 1].type - 1);
        }
      },
      itemCount: length,
    ));
  }
}
